<?php
    error_reporting(E_ALL || ~E_DEPRECATED || ~E_STRICT || ~E_NOTICE || ~E_WARNING || ~E_CORE_WARNING);
//取JsonString、Picture
	$deresult=file_get_contents('php://input');
    $string = "\n deresult:".$deresult."\n";
    file_put_contents("test.log",$string, FILE_APPEND);
	$data=@$_POST["JsonString"];
//	file_put_contents("test.log","\n 1: ".$data."\n", FILE_APPEND); 	
    $accessrecord=json_decode($data,true);
	if(!$accessrecord)
	{
	    die('Could not connect: '.json_last_error());
		$string = "Could not connect: ".json_last_error();
        file_put_contents("test.log", $string, FILE_APPEND);
//		echo "{\"res\":\"error\"}";
		echo "{\"result\":\"ERROR\"}";
		return;
	}
	
//	$string = "\n 0".$accessrecord["device_id"]." 1".$accessrecord["realtime_type"]." 2".$accessrecord["idcard"]. " 3".$accessrecord["gate"] ." 4".$accessrecord["time"]."\n";
//	file_put_contents("test.log", $string, FILE_APPEND);
	
	$access=json_decode($data,true);
	if($accessrecord["gate"]===""||$accessrecord["device_id"]===""||$accessrecord["time"]===""||$accessrecord["idcard"]==="")
	{
		echo "{\"result\":\"ERROR\"}";
		return;
	}
	if($accessrecord["accessstat"]==="")
	{
		$accessrecord["accessstat"]="pass";
	}
	if($accessrecord["entrance"]==="")
	{
		$accessrecord["entrance"]="in";
	}
	$myconn=@mysql_connect("localhost","root","root");	     
    if(!$myconn)
    {
        die('Could not connect: '.mysql_error());
	    $string = "Could not connect: ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);
//		echo "{\"res\":\"error\"}";
		echo "{\"result\":\"ERROR\"}";
		return;
    }
		
	$db_selected=mysql_select_db("dwaccessDB",$myconn);
	if(!$db_selected)
    {
        die("Can't use table : ".mysql_error());
	    $string = "Can't use table : ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);
//		echo "{\"res\":\"error\"}";
		echo "{\"result\":\"ERROR\"}";
		return;
    }
	$tempFile = $_FILES['Picture']['tmp_name'];
//	$tempFile = $_FILES['Picture']['filename'];
//	$Filesize = $_FILES['Picture']['size']/1024;
	$targetPath = dirname(__FILE__). "\\pictures\\";
	$accesstime=strftime('%Y-%m-%d %H:%M:%S',strtotime($accessrecord["time"]));
//	file_put_contents("test.log","\n tempFile: ".$tempFile."   Filesize  ".$Filesize."\n", FILE_APPEND);	
/*   	$strSql="SELECT  MAX(tkid) FROM accessrecord WHERE controlerNum = '".$accessrecord["device_id"]."'AND cardnum='".$accessrecord["idcard"]."'AND gatenum='".$accessrecord["gate"]."'AND CreatedTime='".$accesstime."';";
	$result=mysql_query($strSql,$myconn);
	$dbResult = mysql_fetch_array($result);
//	$string = "\n strSql:".$strSql."result : ".$dbResult[0]."\n";
//	file_put_contents("test.log", $string, FILE_APPEND);	
   	if($dbResult[0])
	{
//	    $strSql="UPDATE accessrecord SET CreatedTime= '".strftime('%Y-%m-%d %H:%M:%S',$accessrecord[4])."' WHERE controlerNum = '".$accessrecord[0]."',cardnum=\"".$accessrecord[2]."\",gatenum=\"".$accessrecord[3]."\";";
//	    $result=mysql_query($strSql,$myconn);
//		@mysql_free_result($result);
//		$string = "\n strSql:".$strSql."\n";
//		file_put_contents("test.log", $string, FILE_APPEND);
	}else
	{*/
	$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance) VALUES('".$accessrecord["device_id"]."','".$accessrecord["idcard"]."','".$accessrecord["gate"]."','".$accesstime."','".$accessrecord["accessstat"]."','".$accessrecord["entrance"]."');";
	$result=mysql_query($strSql,$myconn);
	@mysql_free_result($result);
//	file_put_contents("test.log","$strSql: ".$strSql."\n", FILE_APPEND);
//	$strSql="SELECT tkid FROM accessrecord WHERE controlerNum = '".$accessrecord["device_id"]."'AND cardnum='".$accessrecord["idcard"]."'AND gatenum='".$accessrecord["gate"]."'AND CreatedTime='".$accesstime."';";		
	$strSql = "SELECT MAX(tkid) FROM accessrecord WHERE controlerNum = '".$accessrecord["device_id"]."' AND cardnum='".$accessrecord["idcard"]."' AND gatenum='".$accessrecord["gate"]."' AND CreatedTime='".$accesstime."';";
	$result=mysql_query($strSql,$myconn);
	$dbResult1 = mysql_fetch_array($result);
//		$string = "\n strSql:".$strSql." tkid:".$dbResult1[0]."\n";
//		file_put_contents("test.log", $string, FILE_APPEND);
	@mysql_free_result($result);
	//关闭对数据库的连接
    mysql_close($myconn);
	echo "{\"result\":\"OK\"}";
	if($dbResult1[0])
	{
		$targetFile =$dbResult1[0].".jpg";			
		move_uploaded_file($tempFile,$targetPath.$targetFile);
//			$string = "\n tempFile:".$tempFile."    targetPath.targetFile:  ".$targetPath.$targetFile."... \n";
//			file_put_contents("test.log", $string, FILE_APPEND);
	}	
//	    $string = "strSql:".$strSql."\n";
//	    file_put_contents("test.log", $string, FILE_APPEND);	
//	}
	
/*	$accesshmstime=strftime('%H:%M:%S',strtotime($accessrecord["time"]));
//	$strSql="SELECT MAX(skid) FROM staffbasesetting WHERE (cardnum='".$accessrecord["idcard"]."') AND ((gatenum = 'all') OR (gatenum ='".$accessrecord["gate"]."')) AND ('".$accesstime."' BETWEEN 'periodofvalitystart' AND 'periodofvalityend') AND ('".$accesshmstime."' BETWEEN 'entrancevalitystart' AND 'entrancevalityend') AND ((select controlmethod from gatesetting where gatenum='".$accessrecord["gate"]."')='card');";
	$strSql="SELECT MAX(skid) FROM staffbasesetting WHERE (cardnum='".$accessrecord["idcard"]."') AND ((gatenum = 'all') OR (gatenum ='".$accessrecord["gate"]."'))";	
	$string = "strSql:".$strSql."\n";
//    file_put_contents("test.log", $string, FILE_APPEND);
	$result=mysql_query($strSql,$myconn);
//	$string = "strSql:".$strSql."\n";
//    file_put_contents("test.log", $string, FILE_APPEND);
	$dbResult2 = mysql_fetch_array($result);
//	$string = "strSql:".$strSql."result : ".$dbResult2[0]."\n";
//    file_put_contents("test.log", $string, FILE_APPEND);	
	if($dbResult2[0])
	{*/
	
//		file_put_contents("test.log", "{\"result\":\"OK\",\"pass\":\"access\"} \n", FILE_APPEND);
/*	}else
	{
		echo "{\"result\":\"OK\",\"pass\":\"reject\"}";
//		file_put_contents("test.log", "{\"result\":\"OK\",\"pass\":\"reject\"} \n", FILE_APPEND);
	}
	@mysql_free_result($result);*/
	
//	echo "{\"result\":\"OK\"}";	
//    return;
	exit(0);
?> 
